﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace TeamBuy.AccessWebUI
{
    public static class TeamBuySession
    {
        
        /// <summary>
        /// Is user logged in
        /// </summary>
        public static bool IsUserLoggedIn
        {
            get
            {
                return GetIsUserLoggedIn();
            }
        }

        public static int CreateUserSession(string userEmail, string userPwd, bool isRememberMe)
        {
            TeamBuy.AccessService.TeamBuyUserService userLogin = new AccessService.TeamBuyUserService();
            int userID = userLogin.UserLogin(userEmail, userPwd);
            if (userID > 0)
            {
                DataTable dtCUser = userLogin.GetUser(userID);

                if (isRememberMe == true)
                {
                    SaveLoginCookie(Convert.ToInt32(dtCUser.Rows[0]["UserID"]), dtCUser.Rows[0]["UserEmail"].ToString(), dtCUser.Rows[0]["UserPwd"].ToString(), dtCUser.Rows[0]["UserName"].ToString(), Convert.ToInt32(dtCUser.Rows[0]["CityID"]), dtCUser.Rows[0]["CityName"].ToString());
                }
                HttpContext.Current.Session["UserID"] = dtCUser.Rows[0]["UserID"].ToString();
                HttpContext.Current.Session["UserEmail"] = dtCUser.Rows[0]["UserEmail"].ToString();
                HttpContext.Current.Session["UserPwd"] = dtCUser.Rows[0]["UserPwd"].ToString();
                HttpContext.Current.Session["UserName"] = dtCUser.Rows[0]["UserName"].ToString();
                HttpContext.Current.Session["CityID"] = dtCUser.Rows[0]["CityID"].ToString();
                HttpContext.Current.Session["CityName"] = dtCUser.Rows[0]["CityName"].ToString();
                return userID;

            }
            else if (userID == -1)
            {
                return -1;
            }
            else
            {
                return 0;
            }
        }

        public static bool UpdateCurrentUserSession()
        {
            TeamBuy.AccessService.TeamBuyUserService userUpdate = new AccessService.TeamBuyUserService();

            if (Convert.ToInt32(HttpContext.Current.Session["UserID"]) > 0)
            {
                DataTable dtCUser = userUpdate.GetUser(Convert.ToInt32(HttpContext.Current.Session["UserID"]));

                HttpContext.Current.Session["UserID"] = dtCUser.Rows[0]["UserID"].ToString();
                HttpContext.Current.Session["UserEmail"] = dtCUser.Rows[0]["UserEmail"].ToString();
                HttpContext.Current.Session["UserPwd"] = dtCUser.Rows[0]["UserPwd"].ToString();
                HttpContext.Current.Session["UserName"] = dtCUser.Rows[0]["UserName"].ToString();
                HttpContext.Current.Session["CityID"] = dtCUser.Rows[0]["CityID"].ToString();
                HttpContext.Current.Session["CityName"] = dtCUser.Rows[0]["CityName"].ToString();

                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool DeleteUserSession()
        {
            if (HttpContext.Current.Session["UserID"] != null)
            {
                HttpContext.Current.Session["UserID"] = null;
                HttpContext.Current.Session["UserEmail"] = null;
                HttpContext.Current.Session["UserPwd"] = null;
                HttpContext.Current.Session["UserName"] = null;
                HttpContext.Current.Session["CityID"] = null;
                HttpContext.Current.Session["CityName"] = null;
                HttpCookie ckExpires = HttpContext.Current.Request.Cookies[TeamBuy.AccessService.TeamBuySystemInfo.SiteDomain + "TeamBuyUserStatus"];
                ckExpires.Values.Remove("TeamBuyUserStatus_UserID");
                ckExpires.Values.Remove("TeamBuyUserStatus_UserEmail");
                ckExpires.Values.Remove("TeamBuyUserStatus_UserPwd");
                ckExpires.Values.Remove("TeamBuyUserStatus_UserUserName");
                ckExpires.Values.Remove("TeamBuyUserStatus_CityID");
                ckExpires.Values.Remove("TeamBuyUserStatus_CityName");
                //TimeSpan ts = new TimeSpan(0, 0, 0, 0);
                //ckExpires.Expires = DateTime.Now.Add(ts);
                HttpContext.Current.Response.Cookies.Add(ckExpires);
            }
            return true;
        }

        private static bool GetIsUserLoggedIn()
        {
            if (HttpContext.Current.Session["UserID"] != null &&
                HttpContext.Current.Session["UserEmail"] != null &&
                HttpContext.Current.Session["UserPwd"] != null &&
                HttpContext.Current.Session["UserName"] != null &&
                HttpContext.Current.Session["CityID"] != null &&
                HttpContext.Current.Session["CityName"] != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //Cookie
        public static bool SaveLoginCookie(int userID, string userEmail, string userPwd, string userName, int cityID, string cityName)
        {
            HttpCookie ckLogin;
            if (HttpContext.Current.Request.Cookies[TeamBuy.AccessService.TeamBuySystemInfo.SiteDomain + "TeamBuyUserStatus"] != null)
            {
                ckLogin = HttpContext.Current.Request.Cookies[TeamBuy.AccessService.TeamBuySystemInfo.SiteDomain + "TeamBuyUserStatus"];
            }
            else
            {
                ckLogin = new HttpCookie(TeamBuy.AccessService.TeamBuySystemInfo.SiteDomain + "TeamBuyUserStatus");
            }
            ckLogin.Values.Remove("TeamBuyUserStatus_UserID");
            ckLogin.Values.Remove("TeamBuyUserStatus_UserEmail");
            ckLogin.Values.Remove("TeamBuyUserStatus_UserPwd");
            ckLogin.Values.Remove("TeamBuyUserStatus_UserUserName");
            ckLogin.Values.Remove("TeamBuyUserStatus_CityID");
            ckLogin.Values.Remove("TeamBuyUserStatus_CityName");
            ckLogin.Values.Add("TeamBuyUserStatus_UserID", userID.ToString());
            ckLogin.Values.Add("TeamBuyUserStatus_UserEmail", userEmail.ToString());
            ckLogin.Values.Add("TeamBuyUserStatus_UserPwd", userPwd.ToString());
            ckLogin.Values.Add("TeamBuyUserStatus_UserUserName", userName.ToString());
            ckLogin.Values.Add("TeamBuyUserStatus_CityID", cityID.ToString());
            ckLogin.Values.Add("TeamBuyUserStatus_CityName", cityName.ToString());
            ckLogin.Expires = DateTime.Now.AddDays(30);
            HttpContext.Current.Response.AppendCookie(ckLogin);
            return true;
        }

        public static bool LoadLoginCookie()
        {
            HttpCookie ckLogin = HttpContext.Current.Request.Cookies[TeamBuy.AccessService.TeamBuySystemInfo.SiteDomain + "TeamBuyUserStatus"];
            if (ckLogin != null)
            {
                HttpContext.Current.Session["UserID"] = Convert.ToString(ckLogin.Values["TeamBuyUserStatus_UserID"]);
                HttpContext.Current.Session["UserEmail"] = Convert.ToString(ckLogin.Values["TeamBuyUserStatus_UserEmail"]);
                HttpContext.Current.Session["UserPwd"] = Convert.ToString(ckLogin.Values["TeamBuyUserStatus_UserPwd"]);
                HttpContext.Current.Session["UserName"] = Convert.ToString(ckLogin.Values["TeamBuyUserStatus_UserUserName"]);
                HttpContext.Current.Session["CityID"] = Convert.ToString(ckLogin.Values["TeamBuyUserStatus_CityID"]);
                HttpContext.Current.Session["CityName"] = Convert.ToString(ckLogin.Values["TeamBuyUserStatus_CityName"]);
                return true;
            }
            return false;
        }

        public static bool SaveDefaultCityCookie(int cityID)
        {
            HttpCookie ckLogin;
            if (HttpContext.Current.Request.Cookies[TeamBuy.AccessService.TeamBuySystemInfo.SiteDomain + "TeamBuyUserStatus"] != null)
            {
                ckLogin = HttpContext.Current.Request.Cookies[TeamBuy.AccessService.TeamBuySystemInfo.SiteDomain + "TeamBuyUserStatus"];
            }
            else
            {
                ckLogin = new HttpCookie(TeamBuy.AccessService.TeamBuySystemInfo.SiteDomain + "TeamBuyUserStatus");
            }
            ckLogin.Values.Remove("TeamBuyUserStatus_CurrentCityID");
            ckLogin.Values.Add("TeamBuyUserStatus_CurrentCityID", cityID.ToString());
            ckLogin.Expires = DateTime.Now.AddDays(30);
            HttpContext.Current.Response.AppendCookie(ckLogin);
            return true;
        }

        public static int GetDefaultCityCookie()
        {
            HttpCookie ckLogin = HttpContext.Current.Request.Cookies[TeamBuy.AccessService.TeamBuySystemInfo.SiteDomain + "TeamBuyUserStatus"];
            if (ckLogin != null)
            {
                HttpContext.Current.Session["CurrentCityID"] = Convert.ToString(ckLogin.Values["TeamBuyUserStatus_CurrentCityID"]);
                string test = HttpContext.Current.Session["CurrentCityID"].ToString();
                return Convert.ToInt32(HttpContext.Current.Session["CurrentCityID"].ToString());
            }
            return TeamBuy.AccessService.TeamBuySystemInfo.DefaultCityID;
        }

        public static int SaveInviteUserID(int userID)
        {
            HttpContext.Current.Session["InviteUserID"] = userID;
            return userID;
        }

        public static int GetInviteUserID()
        {
            return Convert.ToInt32(HttpContext.Current.Session["InviteUserID"]);
        }


        //admin

        public static bool IsAdminLoggedIn
        {
            get
            {
                return GetIsAdminLoggedIn();
            }
        }

        public static int CreateAdminSession(string adminName, string adminPwd)
        {
            TeamBuy.AccessService.TeamBuyAdminService adminLogin = new AccessService.TeamBuyAdminService();
            int adminID = adminLogin.AdminLogin(adminName, adminPwd);
            if (adminID > 0)
            {
                DataTable dtAdmin = adminLogin.GetAdmin(adminID);

                HttpContext.Current.Session["AdminID"] = dtAdmin.Rows[0]["AdminID"].ToString();
                HttpContext.Current.Session["AdminName"] = dtAdmin.Rows[0]["AdminName"].ToString();
                HttpContext.Current.Session["AdminPwd"] = dtAdmin.Rows[0]["AdminPwd"].ToString();
                HttpContext.Current.Session["Flag"] = dtAdmin.Rows[0]["Flag"].ToString();
                return adminID;

            }
            else if (adminID == -1)
            {
                return -1;
            }
            else
            {
                return 0;
            }
        }

        public static bool DeleteAdminSession()
        {
            if (HttpContext.Current.Session["AdminID"] != null)
            {
                HttpContext.Current.Session["AdminID"] = null;
                HttpContext.Current.Session["AdminName"] = null;
                HttpContext.Current.Session["AdminPwd"] = null;
                HttpContext.Current.Session["Flag"] = null;
            }
            return true;
        }

        private static bool GetIsAdminLoggedIn()
        {
            if (HttpContext.Current.Session["AdminID"] != null &&
                HttpContext.Current.Session["AdminName"] != null &&
                HttpContext.Current.Session["AdminPwd"] != null &&
                HttpContext.Current.Session["Flag"] != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}